package controllers;

import java.util.Date;
import java.util.List;

import models.Event;
import models.User;
import models.Event.Category;
import play.data.validation.Required;
import play.mvc.Controller;

public class EventManager extends Controller {

	/************************ Create and manage event ******************************/

	public static void category() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}
	
	public static void categories(final String header) {
		Category primaryCategory = Category.valueOf(header);
		final List<Category> subCategories = primaryCategory.getChildren();
		render(subCategories);
	}

	public static void chooseCategory(Category category) {
		createevent(category);
	}

	@Check("NormalUser")
	public static void createevent(Category category) {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			renderArgs.put("category", category);
			render(user);
		}
	}

	public static void addEvent(Category category, @Required String title, @Required String description, @Required Date date, @Required String location) {
		if(validation.hasErrors()) {
			flash.error("Please fill all the fields.");
			params.flash();
			validation.keep();
			createevent(category);
		}
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			Event event = user.addEvent(title, description, category, date, location);
			detailedevent(event.getId());
		}
	}

	public static void myevents() {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			render(user);
		}	
	}

	public static void editevent(Long eventId) {
		User user = User.find("byEmail", session.get("username")).first();
		Event event = Event.findById(eventId);
		render(event, user);
	}
	
	public static void sendInvitation (long eventId) {
		String userName = params.get("Users");
		User userTo = User.find("byUserName", userName).first();
		Event event = Event.findById(eventId);
		userTo.addInvitation(event);
		System.out.println("Send invititation  to user " + userTo.userName + " invitations " + userTo.invitations);
		detailedevent(eventId);
	}
	
	public static void modifyevent(long eventId, @Required String title, @Required String description, @Required Category category, @Required Date date, @Required String location) {
		if(validation.hasErrors()) {
			flash.error("Please fill all the fields.");
			params.flash();
			validation.keep();
			createevent(category);
		}
		Event event = Event.findById(eventId);
		event.title = title;
		event.description = description;
		event.category = category;
		event.categoryString = category.toString().toLowerCase();
		event.eventDate = date;
		event.location = location;
		event.save();
		detailedevent(event.getId());
	}

	public static void deleteEvent(long eventId) {
		Event event = Event.findById(eventId);
		event.delete();
		myevents();
	}
	
	public static void registeredevents() {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			render(user);
		}
	}
	
	/************************* Detailed Event ****************************/

	public static void detailedevent(Long eventId) {
		User user = User.find("byEmail", session.get("username")).first();
		Event event = Event.findById(eventId);		
		
		if(user==null) {
			render(event);
		}
		else {
			List<User> users = User.findAll();
			users.remove(user);
			render(event, user, users);
		}
	}


	//add comments
	public static void postComment(Long eventId, String content) {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			Event event = Event.findById(eventId);
			event.addComment(user, content);
			detailedevent(eventId);
		}
	}
	
	//register to event
	public static void registerevent(Long eventId) {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			Event event = Event.findById(eventId);
			user.registerToEvent(event);
			detailedevent(eventId);
		}
	}

	public static void unregisterevent(Long eventId) {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			Event event = Event.findById(eventId);
			user.unregisterToEvent(event);
			detailedevent(eventId);
		}
	}
	
	public static void rejectevent(Long eventId) {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {
			Event event = Event.findById(eventId);
			user.invitations.remove(event);
			user.save();
			Application.home();
		}
	}
	
	public static void selectedDetailedevent(Long eventId) {
		Event event = Event.findById(eventId);
		event.selected = true;
		event.save();
		Event.em().flush();
		detailedevent(eventId);		
	}

}
